define(function () {
    'use strict';

    function ctrl($scope,$ionicPopup,$stateParams,LocalCacheService,InfoService,HistoryNavChartService,WebService,$ionicLoading,CommonService) {

        $scope.channel = {};

        $scope.$on('$ionicView.beforeEnter', function() {
            $scope.showAds();
            init();
        });


        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        function init(){
            if(!$scope.ads){
                $ionicLoading.show();
            }
            $scope.channel.channelId = $stateParams['id'];
            $scope.charts = new Array();
            var indexCodes = $scope.channel.title.split(",");
            indexCodes.forEach(function(indexCode){
                $scope.charts[indexCode] = {};
                $scope.charts[indexCode].config = HistoryNavChartService.historyIndexChart(null);

            });
            //查询产品收益
            $scope.indexJson = new Array();
            return queryIndexProfit();
        }

        $scope.showAds = function(type){
            WebService.wtAd("xcm_QDIITeam_1").then(function(data){
                if(data) {
                    $scope.ads = data;
                    var adKey = "ad_" + $scope.channel.adId;
                    if((type || !LocalCacheService.get(adKey)) && $scope.ads.length > 0) {
                        var myPopup = $ionicPopup.show({
                            cssClass: "kDialog",
                            templateUrl: "product/ads.html",
                            scope: $scope,
                            buttons: [
                                { text: '确定' }
                            ]
                        });
                        LocalCacheService.set(adKey, true);
                    }
                }
            });
        }

        function queryIndexProfit(){
            return InfoService.getChannelInfosByParentId($scope.channel.channelId).then(function(data){
                if(data){
                    for(var i=0; i< data.length; i++) {
                        var channel = data[i];
                        var channelJson = {};
                        channelJson.channelId = channel.id;
                        channelJson.title = channel.remark;
                        channelJson.titleImg = channel.small_icon;
                        channelJson.name = channel.catalog_name;
                        channelJson.description = channel.description;
                        channelJson.rrInSingleYear = "";
                        channelJson.rrInSixMonth = "";
                        channelJson.funds = new Array();
                        channelJson.isOpen = false; // 是否展开
                        $scope.indexJson.push(channelJson);
                    }
                    InfoService.getIndexProfit($scope.channel.title).then(function(data){
                        if(data){
                            //对指数栏目数据排序
                            sortIndexChannel(data);
                            //控制基金概况html
                            controlConHtml();
                        }
                    });
                }
                $ionicLoading.hide();
            });
        }

        $scope.openItem = function($index, isChart){
            var item = $scope.indexJson[$index];
            var isOpen = item.isOpen;
            if(isOpen) {
                item.isOpen = false;
            } else{
                for(var i=0; i< $scope.indexJson.length; i++) {
                    var channelJson = $scope.indexJson[i];
                    if(i == $index) {
                        channelJson.isOpen = true;
                    } else {
                        channelJson.isOpen = false;
                    }
                }
                if(isChart) {
                    controlIndexChart(item.title);
                }
            }
        };

        var sortIndexChannel = function(data){
            for(var i=0;i<data.length;i++){
                var fund = data[i];
                var indexJson = $scope.indexJson;
                for(var j=0;j<indexJson.length;j++){
                    if(indexJson[j].title == fund.index_code){
                        indexJson[j].rrInSingleYear = fund.price_increase_year;
                        indexJson[j].rrInSixMonth = fund.price_increase_month6;
                    }
                }
            }
            $scope.indexJson = indexJson.sort(function(a, b){ return parseFloat(a.rrInSingleYear) > parseFloat(b.rrInSingleYear) ? -1 : 1;});
        };

        var controlConHtml = function(){
            InfoService.getChildCatalogProds($scope.channel.channelId).then(function(data){
                if(data){
                    for(var i=0;i<data.length;i++){
                        var fund = data[i];
                        var indexJson = $scope.indexJson;
                        for(var j=0;j<indexJson.length;j++){
                            if(indexJson[j].channelId == fund.prod_xcm_type){
                                fund.prod_min_subscribe = CommonService.getMinAmount(fund);
                                indexJson[j].funds.push(fund);
                            }
                        }
                    }
                }
            });
        };

        function controlIndexChart(indexCode){
            InfoService.getHistoryIndex(indexCode,60).then(function(data){
                if(data){
                    //画收盘走势图
                    $scope.charts[indexCode].config = HistoryNavChartService.historyIndexChart(data);
                    $scope.charts[indexCode].date = CommonService.formatDate(data[data.length - 1].data_date);;
                }
            });
        }
    }

    ctrl.$inject = ['$scope','$ionicPopup','$stateParams','LocalCacheService','InfoService','HistoryNavChartService','WebService','$ionicLoading','CommonService'];
    return ctrl;
});